<?php
defined('IN_IA') or exit('Access Denied');

$ops = array('display', 'post', 'delete');
$op = in_array($op, $ops) ? $op : 'display';
$eid = $_GPC['eid'];
if ($op == 'display') {
	$_W['page']['title'] = '活动分类管理 - 活动分类';
	$cateTitle = '添加';

        $category = pdo_fetchall("SELECT * FROM " . tablename('aide_extend_type') . " WHERE id>0 ORDER BY  id ASC");
        $arr=[];
        foreach ($category as $value){
            $arr[$value['id']]=$value;
        }
}
if ($op == 'post') {
	$cateId = intval($_GPC['id']);
	$level = $_GPC['visible_level']?$_GPC['visible_level']:0;//=isfounder为首页分类
	$category = pdo_get('aide_extend_type', array('id' => $cateId), array('parentid'));
	$parent=pdo_get('aide_extend_type', array('id' => $category['parentid']), array('id','name'));
	$faterId = intval($_GPC['fatherid']);
    $parent=pdo_get('aide_extend_type', array('id' => $faterId), array('id','name'));
	$cateTitle = empty($cateId) ? '添加' : '更新';
	$_W['page']['title'] = $cateTitle . '活动分类 - 活动分类';

	if (checksubmit('submit')) {
		if (empty($_GPC['name'])) {
			message('分类名称不能为空');
		}
        $time=date('Y-m-d H:i:s');
		$data = array(
			'name' 			=> $_GPC['name'],
			'parentid' 		=> $category['parentid'],
            'file_path' => $_GPC['file_path'],
            'updated_at' =>$time,
		);
		if (!empty($faterId)) {
			$data['parentid'] = $faterId;
		}
		if (empty($cateId) || !empty($faterId)) {
			$where = array(
				'name' => $_GPC['name'],
				'uniacid' => $_W['uniacid']
			);
			if (!empty($faterId)) $where['parentid'] = $faterId;

			$result = pdo_get('aide_extend_type', $where, array('id'));
			if (!empty($result)) {
				message('分类名称已存在');
			}
			pdo_insert('aide_extend_type', $data);
			$cateId = pdo_insertid();
		} else {
			pdo_update('aide_extend_type', $data, array('id' => $cateId));
			//Util::deleteCache('activity', $id);
		}
        pdo_update('aide_extend_type', ['md5'=>md5($cateId)], array('id' => $cateId));
		message('活动分类更新成功', web_url('extend/type/display', array('id' => $cateId,'eid'=>$eid)), 'success');
	}

	$category = array();
	if (!empty($cateId)) {
		$category = pdo_get('aide_extend_type', array('id' => $cateId));
	}

}

if ($op == 'delete') {
	$category_id = intval($_GPC['cateid']);
	if ($category_id) {
		$category = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('aide_extend_type') . " WHERE id = " . $category_id);
	}
	if (empty($category)) {
		die(json_encode(array('errno' => 1, 'message' => '删除失败: 未指定活动分类.')));
	}

	$pcatetotal = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('aide_activity') . " WHERE parentid = " . $category_id);
	$ccatetotal = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('aide_activity') . " WHERE childid = " . $category_id);
	if ($pcatetotal + $ccatetotal > 0) {
		die(json_encode(array('errno'=>1, 'message'=>'有活动正使用改分类，不可删除！')));
	}
	$child_category_count = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('aide_extend_type') . " WHERE parentid = " . $category_id);
	if ($child_category_count > 0) {
		die(json_encode(array('errno' => 1,'message' => '该分类有子分类, 请先删除子分类')));
	}
	$result = pdo_delete('aide_extend_type', array('id' => $category_id));
	die(json_encode(array('errno' => $result ? 0 : 1, 'message' => $result ? '删除成功' : '删除失败')));
}

include aide_template('extend/type');
